package com.ruoyi.web.order;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.order.domain.OrderDeliveryRoute;
import com.ruoyi.system.order.domain.OrderDetailDTO;
import com.ruoyi.system.order.domain.OrderQueryParams;
import com.ruoyi.system.order.service.OrderDeliveryRouteService;
import com.ruoyi.system.order.service.OrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/orders")
@RequiredArgsConstructor
public class OrderController {
    private final OrderDeliveryRouteService orderDeliveryRouteService;

    private final OrderService orderService;

    @Anonymous
    @GetMapping
    public AjaxResult list(OrderQueryParams queryParams) {
        Page<OrderDetailDTO> page = orderService.pageQuery(queryParams);
        System.out.println(queryParams);
        return AjaxResult.success(page);
    }
    @Anonymous
    @GetMapping("/{id}")
    public AjaxResult detail(@PathVariable Long id) {
        OrderDetailDTO detail = orderService.getDetail(id);
        if (detail == null) {
            return AjaxResult.error("订单不存在");
        }
        return AjaxResult.success(detail);
    }

    @Anonymous
    @Log(title = "订单配送路线", businessType = BusinessType.OTHER)
    @GetMapping("/route")
    public AjaxResult routeByOrderId( Long orderId) {

        // LambdaQuery：只取一条；若无记录返回 null
        OrderDeliveryRoute route = orderDeliveryRouteService.lambdaQuery()
                .eq(OrderDeliveryRoute::getOrderId, orderId)
                .one();

        return AjaxResult.success(route);
    }
}
